package top.mcwebsite.playandroid.business.project.impl.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.AsyncImage
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.playandroid.business.project.impl.model.ProjectModel
import top.mcwebsite.playandroid.business.project.impl.vm.ProjectIntent
import top.mcwebsite.playandroid.business.project.impl.vm.ProjectUiEffect
import top.mcwebsite.playandroid.business.project.impl.vm.ProjectViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn
import top.mcwebsite.playandroid.lib.common.ext.encodeURL
import top.mcwebsite.playandroid.strings.Str

@Composable
fun ProjectPage() {
    val viewModel: ProjectViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is ProjectUiEffect.NavigationToDetailPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.ArticleDetail.PATH + "/${it.projectModel.link.encodeURL()}")
            }
        }
    }
    val lazyPagingItems = viewModel.pagerFlow.collectAsLazyPagingItems()
    EasyLazyListPageColumn(
        lazyPagingItems = lazyPagingItems,
        retry = {
            lazyPagingItems.refresh()
        },
        loadingMoreError = {

        }
    ) { index, item ->
        ProjectItem(projectModel = item) {
            viewModel.sendIntent(ProjectIntent.ClickItemIntent(item))
        }
        if (index < lazyPagingItems.itemCount - 1) {
            Divider(
                modifier = Modifier.padding(horizontal = 12.dp, vertical = 5.dp)
            )
        }
    }
}

@Composable
fun ProjectItem(
    projectModel: ProjectModel,
    onClick: () -> Unit
) {
    Column(
        modifier = Modifier
            .padding(horizontal = 12.dp)
            .clickable { onClick() }
    ) {
        Text(text = projectModel.title, style = MaterialTheme.typography.titleMedium)
        Text(
            text = projectModel.desc,
            style = MaterialTheme.typography.labelMedium,
            color = LocalContentColor.current.copy(0.7f)
        )
        AsyncImage(
            model = projectModel.envelopePic, contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
                .padding(vertical = 5.dp),
            contentScale = ContentScale.Crop,
        )
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = stringResource(id = Str.author_is, projectModel.author),
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(0.7f)
            )
            Text(
                text = projectModel.niceDate, style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(0.7f),
                modifier = Modifier.padding(start = 10.dp)
            )
        }
    }
}